<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { getAllVideos } from '../services/video'

const router = useRouter()
const currentSlide = ref(0)

// 模拟轮播图数据
const carouselItems = [
  {
    id: 1,
    title: '探索电影世界的无限魅力',
    description: '从经典到现代，感受电影带给我们的感动与震撼',
    image: 'https://picsum.photos/id/1019/1200/400',
    link: '/category/all'
  },
  {
    id: 2,
    title: '2024年夏季热门电影前瞻',
    description: '众多大片即将上映，精彩不容错过',
    image: 'https://picsum.photos/id/1024/1200/400',
    link: '/category/all'
  },
  {
    id: 3,
    title: '奥斯卡获奖影片回顾',
    description: '重温经典，感受电影艺术的巅峰之作',
    image: 'https://picsum.photos/id/1000/1200/400',
    link: '/category/all'
  },
  {
    id: 4,
    title: '电影制作幕后揭秘',
    description: '了解一部优秀电影是如何诞生的',
    image: 'https://picsum.photos/id/1001/1200/400',
    link: '/category/all'
  }
]

// 视频数据
const recommendedVideos = ref([])

// 加载视频数据
const loadVideos = () => {
  recommendedVideos.value = getAllVideos().map(video => ({
    id: video.id,
    title: video.title,
    cover: video.cover || `https://picsum.photos/id/${parseInt(video.id) + 1000}/400/225`,
    uploader: video.uploaderName || '视频上传者',
    uploaderAvatar: `https://picsum.photos/id/${parseInt(video.id) + 100}/100/100`,
    views: video.views || '0',
    duration: video.duration || '0分钟',
    category: video.category || '电影'
  }))
  
  // 如果没有视频数据，使用默认数据
  if (recommendedVideos.value.length === 0) {
    recommendedVideos.value = [
      {
        id: '1',
        title: '《奥本海默》深度解析：诺兰的核物理史诗',
        cover: 'https://picsum.photos/id/1035/400/225',
        uploader: '电影前线',
        uploaderAvatar: 'https://picsum.photos/id/1025/100/100',
        views: '3.2万',
        duration: '15:42',
        category: '电影'
      },
      {
        id: '2',
        title: '2024年十大必看电影推荐，绝对不能错过的佳作',
        cover: 'https://picsum.photos/id/1036/400/225',
        uploader: '电影精选',
        uploaderAvatar: 'https://picsum.photos/id/1026/100/100',
        views: '5.7万',
        duration: '22:18',
        category: '电影'
      }
    ]
  }
}

// 轮播图自动播放
let carouselInterval
onMounted(() => {
  startCarousel()
  loadVideos()
})

const startCarousel = () => {
  carouselInterval = setInterval(() => {
    currentSlide.value = (currentSlide.value + 1) % carouselItems.length
  }, 5000)
}

const stopCarousel = () => {
  clearInterval(carouselInterval)
}

// 切换轮播图
const changeSlide = (index) => {
  currentSlide.value = index
  stopCarousel()
  startCarousel()
}

// 导航到视频详情页
const goToVideoDetail = (videoId) => {
  router.push(`/video/${videoId}`)
}

// 导航到分类页面
const goToCategory = (categoryName) => {
  router.push(`/category/${categoryName}`)
}
</script>

<template>
  <div class="home-view">
    <!-- 轮播图区域 -->
    <section class="carousel-section" @mouseenter="stopCarousel" @mouseleave="startCarousel">
      <div class="carousel-container">
        <div class="carousel-wrapper" :style="{ transform: `translateX(-${currentSlide * 100}%)` }">
          <div
            v-for="item in carouselItems"
            :key="item.id"
            class="carousel-item"
            @click="router.push(item.link)"
          >
            <div class="carousel-bg" :style="{ backgroundImage: `url(${item.image})` }"></div>
            <div class="carousel-content">
              <h2 class="carousel-title">{{ item.title }}</h2>
              <p class="carousel-description">{{ item.description }}</p>
            </div>
          </div>
        </div>
        
        <!-- 轮播图指示器 -->
        <div class="carousel-indicators">
          <button
            v-for="(item, index) in carouselItems"
            :key="item.id"
            class="indicator"
            :class="{ active: currentSlide === index }"
            @click.stop="changeSlide(index)"
          ></button>
        </div>
      </div>
    </section>

    <!-- 内容区域 -->
    <div class="content-container">
      <!-- 左侧边栏 - 分类导航 -->
      <aside class="sidebar">
        <nav class="category-nav">
          <h3 class="nav-title">电影分类</h3>
          <ul class="category-list">
            <li class="category-item" @click="goToCategory('all')">
              <span class="category-icon">🎬</span>
              <span class="category-name">全部电影</span>
            </li>
            <li class="category-item" @click="goToCategory('action')">
              <span class="category-icon">💥</span>
              <span class="category-name">动作</span>
            </li>
            <li class="category-item" @click="goToCategory('comedy')">
              <span class="category-icon">😂</span>
              <span class="category-name">喜剧</span>
            </li>
            <li class="category-item" @click="goToCategory('drama')">
              <span class="category-icon">🎭</span>
              <span class="category-name">剧情</span>
            </li>
            <li class="category-item" @click="goToCategory('scifi')">
              <span class="category-icon">🚀</span>
              <span class="category-name">科幻</span>
            </li>
            <li class="category-item" @click="goToCategory('horror')">
              <span class="category-icon">😱</span>
              <span class="category-name">恐怖</span>
            </li>
            <li class="category-item" @click="goToCategory('romance')">
              <span class="category-icon">❤️</span>
              <span class="category-name">爱情</span>
            </li>
            <li class="category-item" @click="goToCategory('thriller')">
              <span class="category-icon">🔪</span>
              <span class="category-name">惊悚</span>
            </li>
          </ul>
        </nav>

        <!-- 排行榜 -->
        <div class="rankings">
          <h3 class="rankings-title">热门排行榜</h3>
          <div class="ranking-list">
            <div v-for="(video, index) in recommendedVideos.slice(0, 5)" :key="video.id" class="ranking-item">
              <span class="rank-number">{{ index + 1 }}</span>
              <div class="ranking-info">
                <div class="ranking-title" @click="goToVideoDetail(video.id)">{{ video.title }}</div>
                <div class="ranking-views">{{ video.views }}次播放</div>
              </div>
            </div>
          </div>
        </div>
      </aside>

      <!-- 主内容 - 推荐视频 -->
      <main class="video-content">
        <div class="section-header">
          <h2 class="section-title">推荐视频</h2>
          <button class="more-btn">更多</button>
        </div>

        <div class="video-grid">
          <div v-for="video in recommendedVideos" :key="video.id" class="video-card" @click="goToVideoDetail(video.id)">
            <div class="video-thumbnail">
              <img :src="video.cover" :alt="video.title" class="thumbnail-img" />
              <span class="video-duration">{{ video.duration }}</span>
            </div>
            <div class="video-info">
              <h3 class="video-title">{{ video.title }}</h3>
              <div class="video-meta">
                <div class="uploader-info">
                  <img :src="video.uploaderAvatar" :alt="video.uploader" class="uploader-avatar" />
                  <span class="uploader-name">{{ video.uploader }}</span>
                </div>
                <div class="video-stats">
                  <span class="view-count">{{ video.views }}次播放</span>
                </div>
              </div>
            </div>
          </div>
        </div>

        <!-- 加载更多按钮 -->
        <div class="load-more">
          <button class="load-more-btn">加载更多</button>
        </div>
      </main>
    </div>
  </div>
</template>

<style scoped>
.home-view {
  min-height: calc(100vh - 60px);
}

/* 轮播图样式 */
.carousel-section {
  position: relative;
  height: 360px;
  overflow: hidden;
  margin-bottom: 30px;
}

.carousel-container {
  position: relative;
  height: 100%;
}

.carousel-wrapper {
  display: flex;
  height: 100%;
  transition: transform 0.5s ease;
}

.carousel-item {
  flex: 0 0 100%;
  position: relative;
  cursor: pointer;
  overflow: hidden;
}

.carousel-bg {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-size: cover;
  background-position: center;
  filter: brightness(0.7);
  transition: transform 0.5s ease;
}

.carousel-item:hover .carousel-bg {
  transform: scale(1.05);
}

.carousel-content {
  position: relative;
  z-index: 2;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 0 40px;
  max-width: 800px;
  color: white;
}

.carousel-title {
  font-size: 36px;
  font-weight: bold;
  margin-bottom: 16px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
}

.carousel-description {
  font-size: 18px;
  margin-bottom: 24px;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
}

.carousel-indicators {
  position: absolute;
  bottom: 20px;
  left: 0;
  right: 0;
  display: flex;
  justify-content: center;
  gap: 12px;
  z-index: 3;
}

.indicator {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.5);
  border: none;
  cursor: pointer;
  transition: all 0.3s ease;
}

.indicator.active {
  background-color: white;
  width: 32px;
  border-radius: 6px;
}

/* 内容容器 */
.content-container {
  display: flex;
  gap: 24px;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

/* 侧边栏样式 */
.sidebar {
  width: 240px;
  flex-shrink: 0;
}

.category-nav {
  background-color: var(--white);
  border-radius: var(--border-radius);
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: var(--shadow-sm);
}

.nav-title {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 16px;
  color: var(--text-primary);
}

.category-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.category-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.category-item:hover {
  background-color: #f5f5f5;
  color: var(--primary-color);
}

.category-icon {
  font-size: 18px;
  width: 24px;
  text-align: center;
}

.category-name {
  font-size: 16px;
  color: var(--text-primary);
}

/* 排行榜样式 */
.rankings {
  background-color: var(--white);
  border-radius: var(--border-radius);
  padding: 20px;
  box-shadow: var(--shadow-sm);
}

.rankings-title {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 16px;
  color: var(--text-primary);
}

.ranking-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.ranking-item {
  display: flex;
  align-items: flex-start;
  gap: 12px;
}

.rank-number {
  width: 24px;
  height: 24px;
  background-color: #f0f0f0;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-secondary);
  flex-shrink: 0;
}

.ranking-item:nth-child(1) .rank-number {
  background-color: #ff4d4f;
  color: white;
}

.ranking-item:nth-child(2) .rank-number {
  background-color: #ffa940;
  color: white;
}

.ranking-item:nth-child(3) .rank-number {
  background-color: #ffec3d;
  color: var(--text-primary);
}

.ranking-info {
  flex: 1;
  min-width: 0;
}

.ranking-title {
  font-size: 14px;
  color: var(--text-primary);
  margin-bottom: 4px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  cursor: pointer;
  transition: color 0.3s ease;
}

.ranking-title:hover {
  color: var(--primary-color);
}

.ranking-views {
  font-size: 12px;
  color: var(--text-light);
}

/* 视频内容区域 */
.video-content {
  flex: 1;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.section-title {
  font-size: 24px;
  font-weight: 600;
  color: var(--text-primary);
}

.more-btn {
  font-size: 14px;
  color: var(--text-secondary);
  transition: color 0.3s ease;
}

.more-btn:hover {
  color: var(--primary-color);
}

/* 视频网格 */
.video-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
  margin-bottom: 30px;
}

.video-card {
  background-color: var(--white);
  border-radius: var(--border-radius);
  overflow: hidden;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  cursor: pointer;
}

.video-card:hover {
  transform: translateY(-4px);
  box-shadow: var(--shadow-md);
}

.video-thumbnail {
  position: relative;
  padding-top: 56.25%; /* 16:9 宽高比 */
  overflow: hidden;
  background-color: #f0f0f0;
}

.thumbnail-img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.5s ease;
}

.video-card:hover .thumbnail-img {
  transform: scale(1.05);
}

.video-duration {
  position: absolute;
  bottom: 8px;
  right: 8px;
  background-color: rgba(0, 0, 0, 0.8);
  color: white;
  padding: 2px 6px;
  border-radius: 3px;
  font-size: 12px;
}

.video-info {
  padding: 12px;
}

.video-title {
  font-size: 16px;
  font-weight: 500;
  color: var(--text-primary);
  margin-bottom: 8px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  line-height: 1.4;
}

.video-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 14px;
  color: var(--text-light);
}

.uploader-info {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
}

.uploader-avatar {
  width: 24px;
  height: 24px;
  border-radius: 50%;
}

.uploader-name {
  font-size: 14px;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.view-count {
  font-size: 12px;
  color: var(--text-light);
}

/* 加载更多按钮 */
.load-more {
  text-align: center;
  padding: 20px 0;
}

.load-more-btn {
  background-color: var(--white);
  color: var(--text-secondary);
  padding: 12px 32px;
  border-radius: 20px;
  font-size: 16px;
  transition: all 0.3s ease;
  border: 1px solid var(--border-color);
}

.load-more-btn:hover {
  background-color: var(--primary-color);
  color: white;
  border-color: var(--primary-color);
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .sidebar {
    display: none;
  }
  
  .video-grid {
    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  }
}

@media (max-width: 768px) {
  .carousel-section {
    height: 240px;
  }
  
  .carousel-content {
    padding: 0 20px;
  }
  
  .carousel-title {
    font-size: 24px;
  }
  
  .carousel-description {
    font-size: 14px;
  }
  
  .content-container {
    padding: 0 15px;
  }
  
  .video-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 15px;
  }
  
  .video-title {
    font-size: 14px;
  }
  
  .section-title {
    font-size: 20px;
  }
}

@media (max-width: 480px) {
  .video-grid {
    grid-template-columns: 1fr;
  }
  
  .carousel-title {
    font-size: 20px;
  }
  
  .carousel-description {
    font-size: 12px;
  }
}
</style>
